package com.zuipin.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.BaseDictionaryData;
import com.zuipin.entity.MemberLevel;
import com.zuipin.entity.product.ProductPrice;
import com.zuipin.mapper.ProductPriceMapper;
import com.zuipin.service.IMemberLevelService;
import com.zuipin.service.IProductPriceService;
import com.zuipin.vo.BackProInfoPrice;
import com.zuipin.vo.PriceTypeVo;
import com.zuipin.vo.ProductPriceVo;

import net.sf.json.JSONObject;

/**
 * 商品价格业务类实现
 *
 * @ClassName ProductPriceServiceImpl
 * @author wangcongyi
 * @date 2017年4月25日 下午8:50:20
 */
@Service
public class ProductPriceServiceImpl implements IProductPriceService {
	
	@Resource
	private IMemberLevelService	memberLevelService;
	
	@Resource
	private ProductPriceMapper	productPriceMapper;
	
	@Override
	public BigDecimal getMemberPrice(Long memberId, Long sysId, BigDecimal price) {
		// TODO Auto-generated method stub
		MemberLevel memberLevel = memberLevelService.findByMemberIdAndSysId(memberId, sysId);
		if (memberLevel != null) {
			BigDecimal decuction = memberLevel.getProductDecuction();
			return price.multiply(decuction).divide(new BigDecimal(10));
		}
		return null;
	}
	
	@Override
	public int updateBackProInfoPrice(ProductPrice record) {
		// TODO Auto-generated method stub
		return productPriceMapper.updateBackProInfoPrice(record);
	}
	
	@Override
	public BigDecimal findSupplyPrice(Long proId) {
		// TODO Auto-generated method stub
		return productPriceMapper.findSupplyPrice(proId);
	}
	
	@Override
	public PriceTypeVo getMemberPrice(Long memberId, Long sysId, BigDecimal price, Long proId) {
		// TODO Auto-generated method stub
		MemberLevel memberLevel = memberLevelService.findByMemberIdAndSysId(memberId, sysId);
		if (null != memberLevel) {
			// 判断是否为代理商 V13
			if ("V13".equals(memberLevel.getLever())) {
				JSONObject params = new JSONObject();
				params.put("sysId", sysId);
				params.put("proId", proId);
				ProductPrice productPrice = productPriceMapper.selectAgentsByProId(params);
				PriceTypeVo priceTypeVo = null;
				if (productPrice != null) {
					priceTypeVo = new PriceTypeVo(true, productPrice.getPrice());
				} else {
					priceTypeVo = new PriceTypeVo(true, price);
				}
				return priceTypeVo;
			} else {
				BigDecimal decuction = memberLevel.getProductDecuction();
				BigDecimal decuctionPrice = price.multiply(decuction).divide(new BigDecimal(10));
				PriceTypeVo priceTypeVo = new PriceTypeVo(false, decuctionPrice);
				return priceTypeVo;
			}
		}
		return null;
	}
	
	@Override
	public int insertSelective(ProductPrice record) {
		// TODO Auto-generated method stub
		return productPriceMapper.insertSelective(record);
	}
	
	@Override
	public int deleteByProId(JSONObject object) {
		// TODO Auto-generated method stub
		return productPriceMapper.deleteByProId(object);
	}
	
	@Override
	public int insertBatch(List<ProductPrice> productPrices) {
		// TODO Auto-generated method stub
		return productPriceMapper.insertBatch(productPrices);
	}
	
	@Override
	public List<ProductPrice> selectByProId(JSONObject object) {
		// TODO Auto-generated method stub
		return productPriceMapper.selectByProId(object);
	}
	
	@Override
	public List<ProductPrice> findByProId(JSONObject object) {
		// TODO Auto-generated method stub
		return productPriceMapper.findByProId(object);
	}
	
	@Override
	public ProductPrice selectAgentsByProId(JSONObject params) {
		// TODO Auto-generated method stub
		return productPriceMapper.selectAgentsByProId(params);
	}
	
	@Override
	public List<BaseDictionaryData> getSetablePriceType(ProductPrice price) {
		// TODO Auto-generated method stub
		return productPriceMapper.getSetablePriceType(price);
	}
	
	@Override
	public List<ProductPrice> getSettledPrice(ProductPrice price) {
		// TODO Auto-generated method stub
		return productPriceMapper.getSettledPrice(price);
	}
	
	@Override
	public List<ProductPriceVo> findByProductId(Long sysId, Long proId) {
		// TODO Auto-generated method stub
		return productPriceMapper.findByProductId(sysId, proId);
	}
	
	/** 
	 * 根据商品类型获取价格
	 * 
	 * @Title: findByType 
	 * @author: wuyicheng
	 * @date: 2017年8月8日下午2:01:33 
	 * @param proId 商品Id
	 * @param sysId 系统ID
	 * @param priceType 商品类型 
	 * @return
	 * @return: ProductPrice
	 * @throws 
	 */
	@Override
	public ProductPrice findByType(Long proId, Long sysId, Long priceType) {
		return productPriceMapper.findByType(proId, sysId, priceType);
	}
	
	@Override
	public List<ProductPrice> findByProSku(JSONObject object) {
		// TODO Auto-generated method stub
		return productPriceMapper.findByProSku(object);
		
	}
	
	/**
	 * 更据商品ID获取不同平台的不同类型价格
	 */
	@Override
	public List<BackProInfoPrice> findProInfoPrice(Long proId) {
		// TODO Auto-generated method stub
		return productPriceMapper.findProInfoPrice(proId);
	}
	
}
